package componentes;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Reynald0
 */
public class CompJoy extends JPanel implements KeyListener, MouseListener
{
   private JLabel  lNorte,lSur,lEste,lOeste;
   private JButton bCentro;
   
   public CompJoy(JPanel pPanel, KeyListener pMovimiento) {
      lNorte = new JLabel("Norte");
      lNorte.setHorizontalAlignment(JLabel.CENTER);
      lNorte.setOpaque(true);
      lNorte.addMouseListener(this);
      lSur = new JLabel("Sur");
      lSur.setHorizontalAlignment(JLabel.CENTER);
      lSur.addMouseListener(this);
      bCentro = new JButton();
      lEste = new JLabel("Este");
      lEste.setHorizontalAlignment(JLabel.CENTER);
      lEste.addMouseListener(this);
      lOeste = new JLabel("Oeste");
      lOeste.setHorizontalAlignment(JLabel.CENTER);
      lOeste.addMouseListener(this);
      this.setLayout(new BorderLayout());
      this.add(lNorte, BorderLayout.NORTH);
      this.add(lSur, BorderLayout.SOUTH);
      this.add(bCentro, BorderLayout.CENTER);
      this.add(lEste, BorderLayout.EAST);
      this.add(lOeste, BorderLayout.WEST);  
      bCentro.addKeyListener(this);
      bCentro.addKeyListener(this);
      this.addKeyListener(this);
      this.addKeyListener(pMovimiento);
      this.setFocusable(true);
      this.requestFocus();
      if(pPanel != null) pPanel.add(this);
   }
   private void reset()
   {
      this.add(lNorte,BorderLayout.NORTH);
      this.add(lSur,BorderLayout.SOUTH);
      this.add(lOeste,BorderLayout.WEST);
      this.add(lEste,BorderLayout.EAST);
      this.add(bCentro,BorderLayout.CENTER);
      doLayout();
   }
   private void accionar(int pKeyCode)
   {
      reset();
      if(pKeyCode==KeyEvent.VK_UP)
      {
         this.add(bCentro,BorderLayout.NORTH);
         this.add(lNorte,BorderLayout.CENTER);
      }
      else if(pKeyCode==KeyEvent.VK_DOWN)
      {
         this.add(bCentro,BorderLayout.SOUTH);
         this.add(lSur,BorderLayout.CENTER);
      }
      else if(pKeyCode==KeyEvent.VK_LEFT)
      {
         this.add(bCentro,BorderLayout.WEST);
         this.add(lOeste,BorderLayout.CENTER);
      }
      else if(pKeyCode==KeyEvent.VK_RIGHT)
      {
         this.add(bCentro,BorderLayout.EAST);
         this.add(lEste,BorderLayout.CENTER);
      }
      doLayout();
      this.requestFocus();
   }
   
   public void keyTyped(KeyEvent pE)
   {
   }

   public void keyPressed(KeyEvent pE)
   {
      accionar(pE.getKeyCode());
   }

   public void keyReleased(KeyEvent pE)
   {
      reset();
   }

   public void mouseClicked(MouseEvent pE)
   {
   }

   public void mousePressed(MouseEvent pE)
   {
      if(lNorte.equals(pE.getSource()))
      {
         accionar(KeyEvent.VK_UP);
      }
      else if(lSur.equals(pE.getSource()))
      {
         accionar(KeyEvent.VK_DOWN);
      }
      else if(lOeste.equals(pE.getSource()))
      {
         accionar(KeyEvent.VK_LEFT);
      }
      else if(lEste.equals(pE.getSource()))
      {
         accionar(KeyEvent.VK_RIGHT);
      }
   }

   public void mouseReleased(MouseEvent pE)
   {
      reset();
   }

   public void mouseEntered(MouseEvent pE)
   {
   }

   public void mouseExited(MouseEvent pE)
   {
   }

}